<template>
    <el-dialog class="add-dia social-serve-add center-add center-1" :title="$store.state.dia.academic.title"
               :visible.sync="$store.state.dia.academic.show">
        <el-form>
            <el-form-item v-for="(item, index) in model" :key="index" label="" label-width="0px">
                <el-select class="is-search" v-if="item.select" v-model="form[item.prop]" placeholder="请选择">
                    <el-option v-for="item1 in item.children" :key="item1" :label="item1" :value="item1"></el-option>
                </el-select>
                <!-- <el-autocomplete
                  class="is-search"
                  v-else-if="item.canSearch"
                  v-model="form[item.prop]"
                  :debounce="600"
                  :fetch-suggestions="getSearchData"
                  :placeholder="'搜索'+item.name"
                  @select="handleSelect"
                >
                </el-autocomplete> -->
                <el-select
                        class="is-search"
                        v-else-if="item.canSearch"
                        v-model="form[item.prop]"
                        multiple
                        filterable
                        remote
                        allow-create
                        reserve-keyword
                        :placeholder="'搜索'+item.name"
                        :remote-method="getSearchData">
                    <el-option
                            v-for="(opt,optIndex) in options"
                            :key="optIndex"
                            :label="opt.label"
                            :value="opt.value">
                    </el-option>
                </el-select>
                <el-input v-model="form[item.prop]" v-else auto-complete="off" :placeholder="item.name"
                          @blur="blur(item)"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="$store.state.dia.academic.show = false">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </span>
    </el-dialog>
</template>
<script>
    export default {
        props: ["model", "form"],
        components: {},
        name: "",
        data() {
            return {
                canAdd: true,
                info: {},
                options: []
                // model:[{prop:"year",name:"年份",width:""},],
            };
        },
        methods: {
            save() {
                if (!this.canAdd) return
                this.canAdd = false
                var path = _g.getRouterPath(this)
                var params = this.form
                params.sta = "1"

                this.$store.state.commonMenu.forEach(ele => {
                    if (ele.children) {
                        params.tablename = ele.children.find(val => val.url == path) ? ele.children.find(val => val.url == path).tablename : params.tablename ? params.tablename : ""
                    } else if (ele.url === path) {
                        params.tablename = ele.tablename
                    }
                });
                for (var key in params) {
                    if (Array.isArray(params[key])) {
                        var namesArray = []
                        var numbersArray = []
                        var orginalArray = []
                        params[key].forEach(ele1 => {
                            if (ele1.toString().indexOf(")") > -1) {
                                namesArray.push((ele1.toString().slice(0, params[key].toString().indexOf("("))))
                                numbersArray.push(/\((.+?)\)/g.exec(ele1.toString())[1])
                            } else {
                                namesArray.push(ele1)
                            }
                        })
                        params[key + "_data"] = [...params[key]]
                        params[key] = namesArray.join(",")
                        params[key + "_number"] = numbersArray
                    }
                }
                // console.log(params)
                // return
                _g.apiPost("teacher/addall", params).then(res => {
                    this.$store.state.dia.academic.show = false
                    _g.toastMsg(res.code == 0 ? "success" : "error", res.msg)
                    this.$emit("reflash")
                    this.form = {
                        add: localStorage.getItem('userName')
                    }
                    setTimeout(() => {
                        this.canAdd = true
                    }, 120)
                })
            },

            blur(item) {
                if (item.blur) item.blur(item)
            },
            /**
             * @name 远程搜索
             */
            getSearchData(queryString) {
                if (queryString) var queryString = queryString.slice(0, queryString.indexOf("("))
                var number = /\((.+?)\)/g.exec(queryString)
                if (number) var queryString = number[1]
                _g.apiPost("teacher/likeauthor", {search: queryString}).then(res => {
                    res.data.forEach(ele => {
                        ele.label = ele.value = `${ele.name}(${ele.number})`
                    })
                    this.$set(this, "options", res.data)
                })
            },

            /**
             * @name 远程搜索选中
             */
            handleSelect(item) {
                console.log(this.form)
            },
            loadData() {
                _g.apiPost("teacher/getselect1", {name: this.tablename}).then(res => {
                    for (let i = 0; i < this.model.length; i++) {
                        if (res.data[this.model[i].prop]) {
                            this.model[i].select = true
                            this.model[i].children = res.data[this.model[i].prop];
                        }
                    }
                })
            }
        },
        created() {
            var path = _g.getRouterPath(this)
            var tablename = ''
            this.$store.state.commonMenu.forEach(ele => {
                if (ele.children) {
                    tablename = ele.children.find(val => val.url == path) ? ele.children.find(val => val.url == path).tablename : tablename ? tablename : ""
                } else if (ele.url === path) {
                    tablename = ele.tablename
                }
            });
            this.tablename = tablename;
            this.loadData()
            this.form.add = localStorage.getItem('userName')
        }
    }
</script>
<style scoped>
    .tmpl {
    }

</style>